﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OfficeBusiness.Model;
using System.Collections.ObjectModel;
using System.Windows.Input;
using System.Windows;

namespace OfficeBusiness.ViewModel
{
    public class EmployeeViewModel : ViewModelBase
    {
        private IBusinessModel businessModel;
        private ObservableCollection<Employee> employeesData;
        private ICommand createCommand;
        private ICommand refreshCommand;
        private ICommand deleteRowCommand;
        private ICommand updateCommand;
        private Employee selectedItem;

        public EmployeeViewModel(IBusinessModel businessModel)
        {
            this.businessModel = businessModel;
            this.GetEmployeesDetails();
            this.businessModel.EmployeeSelectedEvent += new EventHandler(EmployeeSelected);
        }
        
        private void GetEmployeesDetails()
        {
            var empList = this.businessModel.GetEmployeesDetails();
            foreach (var empItem in empList)
            {
                this.EmployeesData.Add(empItem);
            }
        }

        private void EmployeeSelected(object employeeVisibility, EventArgs e)
        {
            this.EmployeesData.Clear();
            this.GetEmployeesDetails();
        }

        public ObservableCollection<Employee> EmployeesData
        {
            get
            {
                if (this.employeesData == null)
                {
                    this.employeesData = new ObservableCollection<Employee>();
                    ////this.employeesData.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(employeesData_CollectionChanged);
                }

                return this.employeesData;
            }
        }

        //private void employeesData_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        //{
        //    switch (e.Action)
        //    {
        //        case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
        //            {
        //                foreach (Employee viewModel in e.NewItems)
        //                {
        //                    if (!initData)
        //                    {
        //                        Employee emp = new Employee();
        //                        emp.DepartmentNumber = viewModel.DepartmentNumber;
        //                        emp.EmployeeNumber = viewModel.EmployeeNumber;
        //                        emp.FirstName = viewModel.FirstName;
        //                        emp.LastName = viewModel.LastName;
        //                        this.newList.Add(emp);
        //                    }
        //                }

        //                break;
        //            }

        //        case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
        //            {
        //                foreach (Employee viewModel in e.OldItems)
        //                {
        //                }

        //                break;
        //            }

        //        case System.Collections.Specialized.NotifyCollectionChangedAction.Reset:
        //            {
        //                break;
        //            }

        //        case System.Collections.Specialized.NotifyCollectionChangedAction.Replace:
        //            {
        //                break;
        //            }
        //    }
        //}

        public Employee SelectedItem
        {
            get
            {
                return this.selectedItem;
            }

            set
            {
                this.selectedItem = value;
                OnPropertyChanged("SelectedItem");
            }
        }

        public ICommand CreateCommand
        {
            get
            {
                if (this.createCommand == null)
                {
                    this.createCommand = new RelayCommand(param => this.CreateRow(), param => true);
                }

                return this.createCommand;
            }
        }

        public ICommand RefreshCommand
        {
            get
            {
                if (this.refreshCommand == null)
                {
                    this.refreshCommand = new RelayCommand(param => this.RefreshTable(), param => true);
                }

                return this.refreshCommand;
            }
        }

        public ICommand DeleteRowCommand
        {
            get
            {
                if (this.deleteRowCommand == null)
                {
                    this.deleteRowCommand = new RelayCommand(param => this.DeleteRow(), param => true);
                }

                return this.deleteRowCommand;
            }
        }

        public ICommand UpdateCommand
        {
            get
            {
                if (this.updateCommand == null)
                {
                    this.updateCommand = new RelayCommand(param => this.Update(), param => true);
                }

                return this.updateCommand;
            }
        }

        private void Update()
        {
            var empList = from e in this.EmployeesData select e;
            bool updateStatus = this.businessModel.UpdateRows<ViewModel.Employee>(empList.ToList());
            if (!updateStatus)
            {
                MessageBox.Show("Update failed");
                this.RefreshTable();
            }
        }

        private void DeleteRow()
        {
            if (this.SelectedItem != null)
            {
                bool deleteStatus = this.businessModel.DeleteSelectedRow<ViewModel.Employee>(this.SelectedItem);
                if (!deleteStatus)
                {
                    MessageBox.Show("Delete failed");
                }
                else
                {
                    this.EmployeesData.Remove(this.SelectedItem);
                }
            }
            else
            {
                MessageBox.Show("Please select a row");
            }
        }

        private void CreateRow()
        {
            var empList = this.businessModel.GetEmployeesDetails();
            var newItems = from e in this.EmployeesData where !(from newItem in empList select newItem.EmployeeNumber).Contains(e.EmployeeNumber) select e;

            bool createStatus = this.businessModel.InsetRows<ViewModel.Employee>(newItems.ToList<ViewModel.Employee>());
            if (!createStatus)
            {
                MessageBox.Show("Create failed");
                this.RefreshTable();
            }
        }

        private void RefreshTable()
        {
            this.EmployeesData.Clear();
            this.GetEmployeesDetails();
        }
    }
}
